package com.hooker.adapter

import android.content.Context
import android.content.Intent
import android.os.Parcelable
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.hooker.bean.TypeInfo
import com.hooker.listener.OnItemClickListener
import com.hooker.penetration.TypeFilterActivity
import com.hooker.settings.Setting
import com.sdk.hooker.penetration.R

/**
 * 详情页的详细item
 */
class TypeItemAdapter : RecyclerView.Adapter<TypeItemAdapter.TypeItemHolder> {

    var context: Context? = null
    var list: MutableList<String> = mutableListOf()
    var listView: RecyclerView? = null
    var mListener: OnItemClickListener? = null

    constructor(
        context: Context?,
        list: MutableList<String>,
        view: RecyclerView,
        listener: OnItemClickListener?
    ) {
        this.context = context
        this.list.addAll(list)
        this.listView = view
        this.mListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TypeItemHolder {
        var view: View = View.inflate(context, R.layout.layout_item_type_filter, null)
        view.layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        return TypeItemHolder(view)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: TypeItemHolder, position: Int) {
        var info = list?.get(position)

        holder?.typeFilterName?.text = info
        holder?.delete?.tag = info
        holder?.delete?.setTag(R.id.position, position)

        holder.delete?.setOnClickListener { view ->
            var pos = view.getTag(R.id.position) as Int
            mListener?.onClick(view, pos)
        }
    }

    fun update(list: MutableList<String>) {
        this.list.clear()
        this.list.addAll(list)
        notifyDataSetChanged()
    }

    fun addItem(text: String) {
        if (!list.contains(text)) {
            list.add(text)
        }
        notifyDataSetChanged()
    }

    fun removeItem(text: String) {
        list.remove(text)
        notifyDataSetChanged()
    }

    class TypeItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var typeFilterName: TextView? = null
        var delete: Button? = null

        init {
            typeFilterName = itemView.findViewById(R.id.tv_filter_name)
            delete = itemView.findViewById(R.id.btn_filter_delete)
        }
    }
}